# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=pyside2-tools
pkgbase=mingw-w64-${_realname}-qt5
pkgname=(${MINGW_PACKAGE_PREFIX}-${_realname}-common-qt5 ${MINGW_PACKAGE_PREFIX}-python{2,3}-${_realname}-qt5)
pkgver=5.11.0
pkgrel=1
pkgdesc="Python bindings for Qt5 (mingw-w64)"
arch=('any')
url="https://doc-snapshots.qt.io/qtforpython/"
license=("LGPL")
source=(https://download.qt.io/official_releases/QtForPython/pyside2/PySide2-${pkgver}-src/pyside-setup-everywhere-src-${pkgver}.tar.xz)
sha256sums=('53129fd85e133ef630144c0598d25c451eab72019cdcb1012f2aec773a3f25be')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-python2-pyside-qt5=${pkgver}"
             "${MINGW_PACKAGE_PREFIX}-python3-pyside-qt5=${pkgver}"
             "${MINGW_PACKAGE_PREFIX}-qt5")
options=('staticlibs' 'strip')

prepare() {
  cd ${srcdir}/pyside-setup-everywhere-src-${pkgver}
}

build() {
  for pyv in python2 python3; do
  local _conf=
    if [ "${pyv}" = "python2" ]; then
      _conf="-DPYTHON_CONFIG_SUFFIX=-python2.7"
    fi
    [[ -d ${srcdir}/build-${MINGW_CHOST}-${pyv} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}-${pyv}
    mkdir -p build-${MINGW_CHOST}-${pyv}
    pushd ${srcdir}/build-${MINGW_CHOST}-${pyv} > /dev/null

    MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX" \
    ${MINGW_PREFIX}/bin/cmake \
      -G"MSYS Makefiles" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=Release \
      ${_conf} \
      -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/${pyv}.exe \
      ../pyside-setup-everywhere-src-${pkgver}/sources/pyside2-tools

    make

    popd > /dev/null
  done

}

package_pyside-tools-common() {
  pkgdesc="PySide2 lupdate, rcc, and uic development tools (Common Files) (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-qt5")

  cd ${srcdir}/build-${MINGW_CHOST}-python3
  make DESTDIR=${pkgdir} install

  rm -rf "${pkgdir}${MINGW_PREFIX}"/lib
  rm -f "${pkgdir}${MINGW_PREFIX}"/bin/pyside2-uic
  rm -f "${pkgdir}${MINGW_PREFIX}"/share/man/man1/pyside2-uic*
}

package_python2-pyside-tools() {
  pkgdesc="PySide2 lupdate, rcc, and uic development tools (for Python 2) (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-pyside2-tools-common-qt5=${pkgver}"
           "${MINGW_PACKAGE_PREFIX}-python2-pyside2-qt5=${pkgver}")

  cd ${srcdir}/build-${MINGW_CHOST}-python2/sources/pyside-tools
  make DESTDIR=${pkgdir} install

  find "${pkgdir}${MINGW_PREFIX}" -type f -exec \
  sed -i -e '1 s|^#! *${MINGW_PREFIX}/bin/python[0-9.]*|#!${MINGW_PREFIX}/bin/python2|' \
         -e '1 s|^#! *${MINGW_PREFIX}/bin/env python[0-9.]*|#!${MINGW_PREFIX}/bin/env python2|' {} +

  mv "${pkgdir}${MINGW_PREFIX}"/bin/{,python2-}pyside2-uic
  mv "${pkgdir}${MINGW_PREFIX}"/share/man/man1/{,python2-}pyside2-uic.1

  rm -rf "${pkgdir}${MINGW_PREFIX}"/lib/python2*/site-packages/pyside2uic/{port_v3,pyside-uic.1}
  rm -f "${pkgdir}${MINGW_PREFIX}"/bin/pyside2-{lupdate,rcc}
  rm -f "${pkgdir}${MINGW_PREFIX}"/share/man/man1/pyside2-{lupdate,rcc}*

  ${MINGW_PREFIX}/bin/python2 -m compileall "${pkgdir}${MINGW_PREFIX}"/lib/python2*/site-packages/pyside2uic/
  ${MINGW_PREFIX}/bin/python2 -O -m compileall "${pkgdir}${MINGW_PREFIX}"/lib/python2*/site-packages/pyside2uic/
}

package_python3-pyside-tools() {
  pkgdesc="PySide2 lupdate, rcc, and uic development tools (for Python 3) (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-pyside2-tools-common-qt5=${pkgver}"
           "${MINGW_PACKAGE_PREFIX}-python3-pyside2-qt5=${pkgver}")

  cd ${srcdir}/build-${MINGW_CHOST}-python3
  make DESTDIR=${pkgdir} install

  rm -rf "${pkgdir}${MINGW_PREFIX}"/lib/python3*/site-packages/pyside2uic/{port_v2,pyside2-uic.1}
  rm -f "${pkgdir}${MINGW_PREFIX}"/bin/pyside2-{lupdate,rcc}
  rm -f "${pkgdir}${MINGW_PREFIX}"/share/man/man1/pyside2-{lupdate,rcc}*

  ${MINGW_PREFIX}/bin/python3 -m compileall "${pkgdir}${MINGW_PREFIX}"/lib/python3*/site-packages/pyside2uic/
  ${MINGW_PREFIX}/bin/python3 -O -m compileall "${pkgdir}${MINGW_PREFIX}"/lib/python3*/site-packages/pyside2uic/
}

# Wrappers
package_mingw-w64-i686-pyside2-tools-common-qt5() {
  package_pyside-tools-common
}

package_mingw-w64-i686-python2-pyside2-tools-qt5() {
  package_python2-pyside-tools
}

package_mingw-w64-i686-python3-pyside2-tools-qt5() {
  package_python3-pyside-tools
}

package_mingw-w64-x86_64-pyside2-tools-common-qt5() {
  package_pyside-tools-common
}

package_mingw-w64-x86_64-python2-pyside2-tools-qt5() {
  package_python2-pyside-tools
}

package_mingw-w64-x86_64-python3-pyside2-tools-qt5() {
  package_python3-pyside-tools
}
